/**
 * 用户管理脚本
 */
(function() {
	/***************************************************************************
	 * 页面初始化
	 */
	// document.onkeydown = function(e) {
	// platform.forbiddenEnter(e);
	// }
	window.onload = function() {
		orderGridView.init();
	};
	/***************************************************************************
	 * orderGridView
	 */
	var orderGridView = {
		init: function() {
			var columns = [{
					checkbox: true,
					field: 'checkbox',
					align: 'center',
					valign: 'middle'
				},
				{
					field: 'order_id',
					title: '订单号',
					width: 120,
					valign: 'middle',
					align: 'center'
				},
				{
					field: 'order_list',
					title: '商品名称',
					width: 120,
					valign: 'middle'
				},
				{
					field: 'address_info',
					title: '订单地址',
					width: 200,
					valign: 'middle'
				},
				{
					field: 'address_person',
					title: '订单联系人',
					width: 80,
					valign: 'middle'
				},
				{
					field: 'address_phone',
					title: '订单电话',
					width: 80,
					valign: 'middle'
				},
				{
					field: 'data_status',
					title: '订单状态',
					width: 180,
					formatter: function(value, data, index) {
						for(var i = 0; i < dictionary.order_statusArray.length; i++) {
							if(dictionary.order_statusArray[i].value == value) {
								return dictionary.order_statusArray[i].text;
							}
						}
					}
				}
			];

			$('#tb_orders').bootstrapTable({
				url: platform.mapUrl('~/rest/order/selectByCond'),
				cache: false,
				method: 'post',
				contentType: 'application/json',
				dataType: 'json',
				search: false,
				strictSearch: true,
				singleSelect: false,
				striped: true,
				showFooter: false,
				showToggle: false,
				showRefresh: true,
				showPaginationSwitch: false,
				silent: true,
				pagination: true,
				sidePagination: 'server',
				pageNumber: 1,
				pageSize: 10,
				pageList: [10, 15, 20, 50],
				queryParams: function(params) {
					return {
						pageNumber: params.offset / params.limit + 1,
						pageSize: params.limit,
						shop_id: methods.getShop().shop_id
					}
				},
				clickToSelect: true,
				height: 504,
				uniqueId: 'order_id',
				userView: false,
				cardView: false,
				toolbar: '#toolbar',
				columns: columns
			});
		}
	};

	ko.applyBindings(orderGridView, document.getElementById('orderList'));

	var buttonGroupView = {
		fnAll: function() {
			$("#tb_orders").bootstrapTable("refresh");
		},
		fnSolved: function() {
			$("#tb_orders").bootstrapTable("refresh", {
				query: {
					data_status: 0
				}
			});
		},

		fnCompound: function() {
			$("#tb_orders").bootstrapTable("refresh", {
				query: {
					data_status: 1
				}
			});
		},
		fnDelivery: function() {
			$("#tb_orders").bootstrapTable("refresh", {
				query: {
					data_status: 2
				}
			});
		},
		fnFinish: function() {
			$("#tb_orders").bootstrapTable("refresh", {
				query: {
					data_status: 3
				}
			});
		},
		fnRefund: function() {
			$("#tb_orders").bootstrapTable("refresh", {
				query: {
					data_status: 4
				}
			});
		},
    };
	ko.applyBindings(buttonGroupView, document.getElementById('button_Group'));
	var toolbarView = {
		/**
		 * 表单提交
		 */
		fnSubmit: function() {
			var selections = $('#tb_orders').bootstrapTable('getSelections');
			if(selections == null || selections == undefined ||
				(selections instanceof Array && selections.length == 0)) {
				$.ikg_showtip('请至少选中一条数据', $.ikg_showtip.TYPE_WAR);
			} else {
				var orders = $('#tb_orders').bootstrapTable('getSelections');
				var params = {};
				var orderIds = [];
				for(var i = 0; i < orders.length; i++) {
					if(orders[i].data_status > 2) {
						$.ikg_showtip('选中的数据中有退款订单或失效订单',
							$.ikg_showtip.TYPE_WAR);
						return;
					}
					orderIds.push(orders[i].order_id);
				}
				params.order_id = orderIds;
				methods.submit(params);
			}
		},
		/**
		 * 表单取消
		 */
		fnCanel: function() {
			var selections = $('#tb_orders').bootstrapTable('getSelections');

			if(selections == null || selections == undefined ||
				(selections instanceof Array && selections.length == 0)) {
				$.ikg_showtip('请至少选中一条数据', $.ikg_showtip.TYPE_WAR);
			} else {
				var orders = $('#tb_orders').bootstrapTable('getSelections');
				var params = {};
				var orderIds = [];
				for(var i = 0; i < orders.length; i++) {
					if(orders[i].data_status != 0) {
						$.ikg_showtip('只有待处理的订单才可以取消', $.ikg_showtip.TYPE_WAR);
						return;
					}
					orderIds.push(orders[i].order_id);
				}
				params.data_status = 5;
				params.order_id = orderIds;
				methods.canel(params);
			}
		},
		fnRefund: function() {
			var selections = $('#tb_orders').bootstrapTable('getSelections');

			if(selections == null || selections == undefined ||
				(selections instanceof Array && selections.length == 0)) {
				$.ikg_showtip('请至少选中一条数据', $.ikg_showtip.TYPE_WAR);
			} else {
				var orders = $('#tb_orders').bootstrapTable('getSelections');
				var params = {};
				var orderIds = [];
				for(var i = 0; i < orders.length; i++) {
					if(orders[i].data_status != 1 && orders[i].data_status != 2) {
						$.ikg_showtip('只有未完成的订餐才可以退款', $.ikg_showtip.TYPE_WAR);
						return;
					}
					orderIds.push(orders[i].order_id);
				}
				params.data_status = 4;
				params.order_id = orderIds;
				methods.canel(params);
			}
		}
	};
	ko.applyBindings(toolbarView, document.getElementById('toolbar'));
	/***************************************************************************
	 * userHeaderView
	 */
	/*
	 * var userHeaderView = { uv_title: ko.observable() };
	 * ko.applyBindings(userHeaderView, document.getElementById('modalHeader'));
	 */
	/***************************************************************************
	 * methods
	 */
	var methods = {
		submit: function(params) {
			platform.servicebreakAsync('~/rest/order/submit', params, 'post')
				.done(
					function(response) {
						if(response.success == true) {
							$.ikg_showtip(response.msg,
								$.ikg_showtip.TYPE_OK);
							$('#tb_orders').bootstrapTable('refresh');

                        } else {
							$.ikg_showtip(response.msg,
								$.ikg_showtip.TYPE_WAR);

                        }
					});
		},
		canel: function(params) {
			platform.servicebreakAsync('~/rest/order/canel', params, 'post')
				.done(
					function(response) {
						if(response.success == true) {
							$.ikg_showtip(response.msg,
								$.ikg_showtip.TYPE_OK);
							$('#tb_orders').bootstrapTable('refresh');

                        } else {
							$.ikg_showtip(response.msg,
								$.ikg_showtip.TYPE_WAR);

                        }
					});
		},
		getShop: function() {
			var param = {};
			param.shop_person = platform.getCurrentUser().user_id;
			var result = JSON2.parse(platform.servicebreak(
				'~/rest/shop/selectOne', param).responseText);
			return result;
		}
	}
})(this);